import tushare as ts
from rx import Observable
import requests
import asyncio
import aiohttp
import re


def get_timely_info(observer, params):
    observer.on_next(requests.get('http://hq.sinajs.cn/list=' + params).text)
    observer.on_completed()


df = ts.get_stock_basics()
source = Observable.from_(df.iterrows())

stock_list = source \
    .map(lambda row: str('sh' + row[0][0:] if row[0][0] == '6' else 'sz' + row[0][0:])) \
    .buffer_with_count(500) \
    .map(lambda li: ','.join(li)) \
    .flat_map(lambda li: Observable.create(lambda observer: get_timely_info(observer, li))) \
    # .subscribe(lambda value: print("Received {0}".format(value)))


# Observable.interval(1000) \
    # .flat_map(lambda x: timely) \
    # .subscribe(lambda val: print("Received {0}".format(val)))
#
# input("Press any key to quit\n")